package com.njlime.setting.repository;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.setting.entities.Announce;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 帮助公告
 * Created by Dean on 2018-1-16.
 */
@Repository
public class AnnounceDaoImpl extends BaseDaoImpl<Announce> {

    public PageBean list(PageBean pageBean, Announce info){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select tin.*,ac.name category_name,u.name creator_name from t_announce tin ");
        sb.append("left join t_announce_category ac on ac.id = tin.category_id ");
        sb.append("left join t_user u on u.id = tin.creator ");
        sb.append("where tin.is_del = 0 ");
        if (info.getPosition() != null) {
            sb.append(" and tin.position = ? ");
            arrList.add(info.getPosition());
        }
        if (info.getType() != null) {
            sb.append(" and tin.type = ? ");
            arrList.add(info.getType());
        }
        if (info.getPublish_status() != null) {
            sb.append(" and tin.publish_status = ? ");
            arrList.add(info.getPublish_status());
        }
        if (info.getCategory_id() != null) {
            sb.append(" and tin.category_id = ? ");
            arrList.add(info.getCategory_id());
        }
        if (!StringUtil.isEmpty(info.getStartTime())) {
            sb.append(" and tin.create_time > ? ");
            arrList.add(info.getStartTime());
        }
        if (!StringUtil.isEmpty(info.getEndTime())) {
            sb.append(" and tin.create_time < ? ");
            arrList.add(info.getEndTime());
        }
        if (!StringUtil.isEmpty(info.getTitle())) {
            sb.append(" and tin.title like concat('%',?,'%') ");
            arrList.add(info.getTitle());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    public void edit(Announce announce){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_announce set update_time = now() ");
        if(!StringUtil.isEmpty(announce.getTitle())){
            sb.append(",title = ?");
            arrList.add(announce.getTitle());
        }
        if(!StringUtil.isEmpty(announce.getContent())){
            sb.append(",content = ?");
            arrList.add(announce.getContent());
        }
        if(announce.getCategory_id() != null){
            sb.append(",category_id = ?");
            arrList.add(announce.getCategory_id());
        }
        if(announce.getPublish_status() != null){
            sb.append(",publish_status = ?");
            arrList.add(announce.getPublish_status());
        }
        if(announce.getPublish_time() != null){
            sb.append(",publish_time = ?");
            arrList.add(announce.getPublish_time());
        }
        if(announce.getPosition() != null){
            sb.append(",position = ?");
            arrList.add(announce.getPosition());
        }
        sb.append(" where id = ? ");
        arrList.add(announce.getId());
        updateJdbc(sb.toString(), arrList);
    }
}
